package com.jcfk.eam.controller.parent;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.exception.CustomException;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.mt.EamMtUseApplyDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.eam.domain.query.base.EamProductAssetModelQuery;
import com.jcfk.eam.domain.dto.base.EamProductAssetModelDTO;
import com.jcfk.eam.domain.uploadExcel.base.EamProductAssetModelUploadExcel;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

import com.jcfk.eam.service.base.EamProductAssetModelService;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.eam.domain.excel.base.EamProductAssetModelExcel;

import java.util.List;

/**
 * 产品与资产型号
 *
 * @author yql
 * @email 522891237@gqq.com
 * @date 2024-07-25
 */
@Api(tags = {"产品与资产型号管理"})
@RestController
@RequestMapping("eamproductassetmodel")
public class EamProductAssetModelController {
    @Autowired
    private EamProductAssetModelService eamProductAssetModelService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamProductAssetModelQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询产品与资产型号")
    @PostMapping("/queryByPage")
    public RetPage<EamProductAssetModelDTO> queryByPage(@RequestBody ReqPageParam<EamProductAssetModelQuery> requestParam) {
        DataPage<EamProductAssetModelDTO> page = new DataPage<EamProductAssetModelDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), EamProductAssetModelDTO.class);
        page = eamProductAssetModelService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }

    /**
     * 产品列表
     */
    @ApiOperation("产品列表")
    @PostMapping("/list")
    public RetResult<EamMtUseApplyDTO> list(@RequestBody @Valid EamProductAssetModelQuery requestParam) {

        EamMtUseApplyDTO eamMtUseApplyDTO = eamProductAssetModelService.selectAllListByNowProductId(requestParam);

        return RetResult.ok(eamMtUseApplyDTO);
    }

    /**
     * 产品列表
     */
    @ApiOperation("产品列表")
    @PostMapping("/list/only")
    public RetResult<List<EamProductAssetModelDTO>> listonly(@RequestBody EamProductAssetModelQuery requestParam) {

        List<EamProductAssetModelDTO> eamMtUseApplyDTO = eamProductAssetModelService.selectAllList(requestParam);

        return RetResult.ok(eamMtUseApplyDTO);
    }


    /**
     * 产品与资产型号
     * 根据id 查询详情
     */
    @ApiOperation("查询产品与资产型号")
    @GetMapping("/get/{id}")
    public RetResult<EamProductAssetModelDTO> get(@PathVariable("id") String id) {
        EamProductAssetModelDTO item = eamProductAssetModelService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理产品与资产型号
     */
    @ApiOperation("保存产品与资产型号")
    @PostMapping("/save")
    @RequestLogHeader(modular = "产品与资产型号管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamProductAssetModelDTO eamProductAssetModel) {
        // 数据校验
        RetResult ret = eamProductAssetModelService.isValid(eamProductAssetModel);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        if (1 == eamProductAssetModel.getEnabled()) {
            EamProductAssetModelQuery query = new EamProductAssetModelQuery();
            query.setProductId(eamProductAssetModel.getProductId());
            query.setAssetTypeId(eamProductAssetModel.getAssetTypeId());
            query.setAssetKind(eamProductAssetModel.getAssetKind());
            List<EamProductAssetModelDTO> getList = eamProductAssetModelService.getList(query);

            if (getList != null && !getList.isEmpty()) {
                for (EamProductAssetModelDTO set : getList) {
                    if (!set.getId().equals(eamProductAssetModel.getId()))
                        throw new CustomException("数据重复，保存失败");
                }
            }
        }

        if (StrUtil.isEmpty(eamProductAssetModel.getId())) {
            // 新增
            eamProductAssetModelService.insert(eamProductAssetModel);
        } else {
            // 更新
            eamProductAssetModelService.update(eamProductAssetModel);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除产品与资产型号
     */
    @ApiOperation("批量删除产品与资产型号")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "产品与资产型号管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamProductAssetModelDTO eamProductAssetModel) {
        return RetResult.ok(eamProductAssetModelService.logicDeleteList(eamProductAssetModel));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamProductAssetModelQuery requestParam, HttpServletResponse response) throws Exception {
        List<EamProductAssetModelDTO> list = eamProductAssetModelService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, EamProductAssetModelExcel.class);
    }

    /**
     * 导入产品与资产型号
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<EamProductAssetModelUploadExcel> items = EasyExcel.read(file.getInputStream(), EamProductAssetModelUploadExcel.class, null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        eamProductAssetModelService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<EamProductAssetModelUploadExcel> list = new ArrayList<>();
        list.add(new EamProductAssetModelUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, EamProductAssetModelUploadExcel.class);
    }


    /**
     * 删除产品与资产型号
     */
    @ApiOperation("删除产品与资产型号")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "产品与资产型号管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamProductAssetModelService.deleteitem(id);
        return RetResult.ok();
    }
}
